/*
 * MConexion.java
 *
 * Created on 5 de septiembre de 2006, 08:55 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package ig.formato.modelo;

import ig.formato.dto.ConexionDTO;
import ig.formato.dto.SgbdDTO;
import ig.formato.gui.app.PConexion;
import ig.basic.util.IModelo;
import ig.formato.util.IGFormatoException;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;

/**
 *
 * @author jsanchez
 */
public class MConexion implements IModelo {

    public static void main(String[] arg) {
        BigDecimal a = new BigDecimal(10.2);
        BigDecimal b = new BigDecimal(10);
        System.out.println(a.scale());
        System.out.println(b.scale());
    }
    PConexion panel = null;
    ig.formato.dao.ConexionDao dao = new ig.formato.dao.ConexionDao();
    ig.formato.dto.ConexionDTO dto = null;
    Collection cc = null;
    /** Creates a new instance of MConexion */

    public MConexion(PConexion panel) {
        this.panel = panel;
    }

    public void guardar() throws IGFormatoException {
        Object myObj = this.dao.existe(this.dto);
        String NM = "Fuente de Base de Dato";
        if (!(myObj instanceof Boolean)) {
            if (this.dto.getId() == null) {
                throw new IGFormatoException("Ya existe una " + NM + " con este nombre", "Cambie el nombre");
            } else if ((myObj instanceof Integer) && !((Integer) myObj).equals(dto.getId())) {
                throw new IGFormatoException("Ya existe una " + NM + " con este nombre", "Cambie el nombre");
            }
        }
        try {
            dao.setAutoCommit(false);
            this.setId(dao.guardar(this.dto));
            dao.commit();
        } catch (IGFormatoException e) {
            dao.rollback();
            throw e;
        }
    }

    public Collection buscar() throws IGFormatoException {
        cc = new ig.formato.dao.SgdbDao().buscarTodos();
        return dao.buscarTodos();
    }

    public void modificar() throws IGFormatoException {
    }

    public void nuevo() throws IGFormatoException {
        dto = new ig.formato.dto.ConexionDTO();
        cc = new ig.formato.dao.SgdbDao().buscarTodos();
    }

    public void eliminar() throws IGFormatoException {
        try {
            dao.setAutoCommit(false);
            dao.eliminar(this.dto.getId());
            dao.commit();
        } catch (IGFormatoException e) {
            dao.rollback();
            throw e;
        }
    }

    public void salir() throws IGFormatoException {
    //        MasterConnection.getMasterConnection().closeConnectionMaster();
    }

    public void imprimir() throws IGFormatoException {
    }

    public Object getTO() throws IGFormatoException {
        return dto;
    }

    public Connection getConectionMaster() throws SQLException, ClassNotFoundException, Exception {
        return this.dao.getConnectionIgFormato();
    }

    public void cancelar() throws IGFormatoException {
        this.setId(this.dto.getId());
    }

    public void setId(Integer id) {
        if (id != null) {
            try {
                dto = (ConexionDTO) this.dao.buscar(id);
            } catch (IGFormatoException ex) {
                ex.printStackTrace();
            }
        }
    }

    /*** metodos adicionales **/
    public Collection getSGDB() {
        return cc;
    }

    public void test(SgbdDTO sgbdDTO) throws ClassNotFoundException, SQLException, IGFormatoException {
        if (!sgbdDTO.getDriver().toUpperCase().trim().equals("JAVA.LANG.OBJECT")) {
            Class.forName(sgbdDTO.getDriver());
            ConexionDTO _dto = (ConexionDTO) panel.getTO();
            String URL = sgbdDTO.getUrl()
                    .replace("<host>", _dto.getHost())
                    .replace("<puerto>", _dto.getPuerto().toString())
                    .replaceAll("<db>", _dto.getBaseDeDatos());
            java.sql.DriverManager.getConnection(URL, _dto.getUsuario(), _dto.getClave()).close();
        } else {
            ConexionDTO _dto = (ConexionDTO) panel.getTO();
            Class.forName(_dto.getHost());
        }
    }

    public void setClonar(boolean flag) {
    }

    public boolean isClonar() {
        return false;
    }
}



